<?php

global $_GPC;
global $_W;
$shopid = check_right(3);
if (!$shopid) {
    message('请先选择需要管理的店铺', $this->createWebUrl('shop'), 'error');
}
$shop = pdo_fetch('SELECT * FROM ' . tablename('j_hmoney_group') . ' WHERE weid = \'' . $_W['uniacid'] . '\' and id=\'' . $shopid . '\'');
$operation = !empty($_GPC['op']) ? $_GPC['op'] : 'display';
$cfg = $this->module['config'];
if ($operation == 'display') {
    $list = pdo_fetchall('SELECT * FROM ' . tablename('j_hmoney_projects') . ' WHERE weid=\'' . $_W['uniacid'] . '\' and groupid=\'' . $shopid . '\' order by pcate asc, id desc ');
    $cateAry = array();
    $category = pdo_fetchall('SELECT * FROM ' . tablename('j_hmoney_projectscate') . ' WHERE weid=\'' . $_W['uniacid'] . '\' and groupid=\'' . $shopid . '\' order by displayorder asc, id desc ');
    foreach ($category as $row) {
        $cateAry[$row['id']] = $row['title'];
    }
    $staffAry = array();
    $staffcate = pdo_fetchall('SELECT * FROM ' . tablename('j_hmoney_staffcate') . ' WHERE weid = \'' . $_W['uniacid'] . '\' and groupid=:a order by id asc', array(':a' => $shopid));
    foreach ($staffcate as $row) {
        $staffAry[$row['id']] = $row['title'];
    }
} else {
    if ($operation == 'post') {
        $id = $_GPC['id'];
        if ($id) {
            $item = pdo_fetch('SELECT * FROM ' . tablename('j_hmoney_projects') . ' WHERE id = :id ', array(':id' => $id));
            $staffcateAry = explode(',', $item['staffcate']);
        }
        $category = pdo_fetchall('SELECT * FROM ' . tablename('j_hmoney_projectscate') . ' WHERE weid=\'' . $_W['uniacid'] . '\' and groupid=\'' . $shopid . '\' order by displayorder asc, id desc ');
        $list = pdo_fetchall('SELECT * FROM ' . tablename('j_hmoney_staffcate') . ' WHERE weid = \'' . $_W['uniacid'] . '\' and groupid=:a order by id asc', array(':a' => $shopid));
        if (checksubmit('submit')) {
            $data = array('weid' => $_W['uniacid'], 'groupid' => $shopid, 'title' => $_GPC['title'], 'pcate' => $_GPC['pcate'], 'price' => $_GPC['price'], 'description' => htmlspecialchars_decode($_GPC['description']), 'staffcate' => implode(',', $_GPC['staffcate']), 'status' => intval($_GPC['status']), 'commission' => trim($_GPC['commission']), 'performance' => trim($_GPC['performance']));
            if (!$id) {
                pdo_insert('j_hmoney_projects', $data);
                $id = pdo_insertid();
            } else {
                unset($data['weid']);
                unset($data['groupid']);
                pdo_update('j_hmoney_projects', $data, array('id' => $id));
            }
            message('修改完成', $this->createWebUrl('projects', array('op' => 'post', 'id' => $id)), 'success');
        }
    } else {
        if ($operation == 'delete') {
            $id = intval($_GPC['id']);
            if ($id) {
                pdo_delete('j_hmoney_projects', array('id' => $id));
            }
            message('删除成功', $this->createWebUrl('projects'), 'success');
        } else {
            if ($operation == 'changecateorder') {
                foreach ($_GPC['displayorder'] as $index => $row) {
                    pdo_update('j_hmoney_projectscate', array('displayorder' => $row), array('id' => $index));
                }
                exit(json_encode(array('success' => true)));
            } else {
                if ($operation == 'categorydisplay') {
                    $category = pdo_fetchall('SELECT * FROM ' . tablename('j_hmoney_projectscate') . ' WHERE weid=\'' . $_W['uniacid'] . '\' and groupid=\'' . $shopid . '\' order by displayorder asc, id desc ');
                } else {
                    if ($operation == 'getcate') {
                        $id = intval($_GPC['id']);
                        if (!$id) {
                            exit(json_encode(array('success' => true, 'item' => '')));
                        }
                        $item = pdo_fetch('SELECT * FROM ' . tablename('j_hmoney_projectscate') . ' WHERE id = :id ', array(':id' => $id));
                        exit(json_encode(array('success' => true, 'item' => $item)));
                    } else {
                        if ($operation == 'categorypost') {
                            $id = intval($_GPC['id']);
                            if (!$_GPC['title']) {
                                exit(json_encode(array('success' => false, 'msg' => '名称标题不能为空')));
                            }
                            $data = array('title' => $_GPC['title'], 'status' => $_GPC['status']);
                            if (!empty($id)) {
                                pdo_update('j_hmoney_projectscate', $data, array('id' => $id));
                            } else {
                                $data = array('weid' => $_W['uniacid'], 'groupid' => intval($shopid), 'title' => $_GPC['title'], 'status' => $_GPC['status']);
                                pdo_insert('j_hmoney_projectscate', $data);
                            }
                            exit(json_encode(array('success' => true)));
                        }
                    }
                }
            }
        }
    }
}
include $this->template('web/' . $cfg['floder'] . '/projects');